package com.dangf.hz.controller;

import java.util.Map;

import com.dangf.hz.entity.User;
import com.dangf.hz.service.UserService;

import pw.hais.servlet.BaseController;

/**
 * 用户控制器
 * @author Hello_海生
 * @date 2014年7月30日
 */
@SuppressWarnings("serial")
public class UserController extends BaseController {
	private UserService userService = new UserService();
	
	/**
	 * 登录方法
	 * @return
	 */
	public Map<String,Object> login(){
		User u = new User();
		u.setUsername(getString("username"));
		u.setPassword(getString("password"));
		//判断账号密码是否为空
		if("".equals(u.getUsername()) || "".equals(u.getPassword())){
			setResult("301", "帐号密码不能为空！", "");
		}else{	//处理登录
			result=userService.login(u);
		}
		return result;
	}
	
	/**
	 * 注册用户
	 * @return
	 */
	public Map<String, Object> register(){
		User u = new User();
		u.setUsername(getString("username"));
		u.setPassword(getString("password"));
		//判断账号密码是否为空
		if("".equals(u.getUsername()) || "".equals(u.getPassword())){
			setResult("301", "帐号密码不能为空！", "");
		}else{	//处理登录
			result=userService.save(u);
		}
		return result;
	}
	
	/**
	 * 获取个人信息
	 * @return
	 */
	public Map<String, Object> show(){
		User u = new User();
		u.setId(getlong("id"));
		if(u.getId()==0){
			setResult("301", "参数不完整！", "");
		}else{
			u.setToken(getString("token"));
			result=userService.show(u);
		}
		return result;
	}

	/**
	 * 更改用户信息
	 * @return
	 */
	public Map<String, Object> update(){
		User u = new User();
		u.setId(getlong("id"));
		u.setToken(getString("token"));
		if(u.getId()==0 || "".equals(u.getToken())){
			setResult("301", "参数不完整！", "");
		}else{
			u.setPassword(getString("password"));
			u.setEmail(getString("email"));
			u.setPhoto(getString("photo"));
			result=userService.update(u);
		}
		return result;
	}
}
